package com.fs.dao.Impl;

import com.fs.dao.DiscusstionDao;
import com.fs.pojo.Discusstion;
import com.fs.utils.JDBCUtils;

import java.util.List;

public class DiscusstionDaoImpl implements DiscusstionDao {

    @Override
    public Discusstion queryById(String id) {

        //language=MySQL
        String sql = "select * from tb_discusstion where  id = ?";

        List<Discusstion> discusstions = JDBCUtils.executeQurey(sql, Discusstion.class, id);

        return  discusstions == null ? null : discusstions.get(0) ;
    }

    @Override
    public List<Discusstion> queryAll() {


        return JDBCUtils.executeQurey("select * from tb_discusstion ", Discusstion.class);
    }

    @Override
    public int insert(Discusstion obj) {

        String sql = "insert into tb_discusstion(id,class_id, user_id, title, content, pulish_time, views, replies) values (?,?,?,?,?,?,?,?)";

        return JDBCUtils.executeUpdate(sql,obj);
    }

    @Override
    public int deleteById(String id) {

        String sql = "delete from tb_discusstion where  id = ?";

        return JDBCUtils.executeUpdate(sql,id);
    }

    @Override
    public int update(Discusstion obj) {

        String sql = JDBCUtils.parseUpdateSQL(obj, "tb_discusstion");


        return JDBCUtils.executeUpdate(sql,obj);
    }

    @Override
    public List<Discusstion> queryAllByClassId(String classId) {
        return JDBCUtils.executeQurey("select * from tb_discusstion where class_id = ?",Discusstion.class,classId);
    }
}
